package com.taiji.oauth2.authorizeserver.config;

import com.taiji.oauth2.authorizeserver.core.service.UserService;
import com.taiji.oauth2.authorizeserver.properties.AuthorizeServerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;


@Configuration
@EnableAuthorizationServer
public class AuthorizeServer extends AuthorizationServerConfigurerAdapter {


    @Autowired
    private AuthorizeServerProperties authorizeServerProperties;


    @Autowired
    private PasswordEncoder passwordEncoder;


    @Autowired
    private AuthenticationManager authenticationManager;




    @Autowired
    private UserService userService;


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
         clients.inMemory().withClient("client-a").secret(passwordEncoder.encode("1234"))
                 .redirectUris("http://localhost:8090/client-a/login")
              .scopes(authorizeServerProperties.getScopes())
                .authorizedGrantTypes(authorizeServerProperties.getGrantTypes())
                .refreshTokenValiditySeconds(authorizeServerProperties.getRefreshTokenValid())
                 .accessTokenValiditySeconds(authorizeServerProperties.getAccessTokenValid())
         .and().withClient("client-b").secret(passwordEncoder.encode("1234"))
                 .redirectUris("http://localhost:8091/client-b/login")
                 .scopes(authorizeServerProperties.getScopes())
                 .authorizedGrantTypes(authorizeServerProperties.getGrantTypes())
         ;
    }



    @Bean
    public TokenStore createTokenStore(){
        return new InMemoryTokenStore();
    }



    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(createTokenStore())
                .approvalStore(approvalStore())
                .authenticationManager(authenticationManager)
                .userDetailsService(userService)
                .reuseRefreshTokens(false);
    }

  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) {
      security.realm("OAuth2-Sample")
              .allowFormAuthenticationForClients()
              .tokenKeyAccess("isAuthenticated()")
              .checkTokenAccess("isAuthenticated()");
    }


    @Bean
    public ApprovalStore approvalStore() throws Exception {
        TokenApprovalStore store = new TokenApprovalStore();
        store.setTokenStore(createTokenStore());
        return store;
    }

}
